﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging.Messages;
using CommunityToolkit.Mvvm.Messaging;
using Serilog;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TMI85303.UI.Messages;
using TMI85303.Utils;


namespace TMI85303.UI.UserControls
{
    /// <summary>
    /// MTP_ReadWriteControl.xaml 的交互逻辑
    /// </summary>
    public partial class MTP_ReadWriteControl : UserControl
    {
        public MTP_ReadWriteControl()
        {
            InitializeComponent();
        }


    }

    public partial class MTP_ReadWriteVM : ObservableObject
    {
        [ObservableProperty]
        private string? readMTPAddr;

        [ObservableProperty]
        private string? readMTPData;

        [ObservableProperty]
        private string? writeMTPAddr;

        [ObservableProperty]
        private string? writeMTPData;

        [ObservableProperty]
        private string? readRegAddr;

        [ObservableProperty]
        private string? readRegData;

        [ObservableProperty]
        private string? writeRegAddr;

        [ObservableProperty]
        private string? writeRegData;

        [ObservableProperty]
        private Utils.DataType readMTPDataType = Utils.DataType.DEC;

        [ObservableProperty]
        private Utils.DataType writeMTPDataType = Utils.DataType.DEC;

        [ObservableProperty]
        private Utils.DataType readRegDataType = Utils.DataType.DEC;

        [ObservableProperty]
        private Utils.DataType writeRegDataType = Utils.DataType.DEC;

        public MTP_ReadWriteVM()
        {
        }

        [RelayCommand]
        private void ReadMTPEE()
        {
            if(ushort.TryParse(ReadMTPAddr, out var valAddr))
            {
                int readData = ReadWrite.ReadEE(valAddr);
                WeakReferenceMessenger.Default.Send<UpdateRegisterMesg, int>(new(valAddr, readData), (int)MesgToken.RegVMToken);
                ReadMTPData = ReadMTPDataType == DataType.DEC ? readData.ToString() : readData.ToString("X4");
            }
            else
            {
                Log.Information("输入地址应该为十进制数！");
                return;
            }
        }

        [RelayCommand]
        private void WriteMTPEE()
        {
            if (ushort.TryParse(WriteMTPAddr, out var valAddr) && 
                int.TryParse(WriteMTPData,
                WriteMTPDataType == DataType.DEC ? NumberStyles.Integer: NumberStyles.HexNumber,
                CultureInfo.InvariantCulture,
                out var valData))
            {
                ReadWrite.WriteEE(valAddr, valData);
                WeakReferenceMessenger.Default.Send<UpdateRegisterMesg, int>(new(valAddr, valData), (int)MesgToken.RegVMToken);
            }
            else
            {
                Log.Information("输入地址或输入的值转化失败！");
                return;
            }
        }

        [RelayCommand]
        private void ReadRegEE()
        {
            if (ushort.TryParse(ReadRegAddr, out var valAddr))
            {
                if (valAddr != 64 && valAddr != 65 && valAddr != 66 && valAddr != 67 && valAddr != 69 &&
                    valAddr != 95 && valAddr != 92 && valAddr != 166 && valAddr != 167 && valAddr != 168 && valAddr != 169)
                {
                    int readData = ReadWrite.ReadReg(valAddr, 2);

                    WeakReferenceMessenger.Default.Send<UpdateRegisterMesg, int>(new(valAddr - 64, readData), (int)MesgToken.RegVMToken);
                    ReadRegData = ReadRegDataType == DataType.DEC ? readData.ToString() : readData.ToString("X4");
                }
                else
                {
                    MessageBox.Show("此寄存器未开放");
                }
            }
            else
            {
                Log.Information("输入地址应该为十进制数！");
                return;
            }
        }

        [RelayCommand]
        private void WriteRegEE()
        {
            if (ushort.TryParse(WriteRegAddr, out var valAddr) &&
                int.TryParse(WriteRegData,
                WriteRegDataType == DataType.DEC ? NumberStyles.Integer : NumberStyles.HexNumber,
                CultureInfo.InvariantCulture,
                out var valData))
            {
                if (valAddr != 64 && valAddr != 65 && valAddr != 66 && valAddr != 67 && valAddr != 69 &&
                    valAddr != 92 && valAddr != 95 && valAddr != 166 && valAddr != 167 && valAddr != 168 && valAddr != 169)
                {
                    ReadWrite.WriteReg(valAddr, valData, 2);
                    WeakReferenceMessenger.Default.Send<UpdateRegisterMesg, int>(new(valAddr - 64, valData), (int)MesgToken.RegVMToken);
                }
                else
                {
                    MessageBox.Show("此寄存器未开放");
                }

            }
            else
            {
                Log.Information("输入地址或输入的值转化失败！");
                return;
            }
        }

    }
}
